import re
from urllib import request


def get_list(page):
    url = 'https://bj.58.com/dashanzi/chuzu/pn'+page+'/'
    
    req = request.Request(url)
    res = request.urlopen(req)

    html = res.read().decode("utf-8")

    #对数据进行解析
    try:
        pat = '<li logr=".*?".*?sortid=".*?">.*?<div class="img_list">.*?<img.*?lazy_src="(.*?)".*?src=".*?">.*?<div class="des">.*?<a href=".*?".*?class="strongbox".*?>(.*?)</a>.*?<p class="room strongbox">(.*?)</p>.*?<b class="strongbox">(.*?)</b>.*?</li>'
        dlist = re.findall(pat,html,re.S)
        # 输出数据
        for v in dlist:
            print('标题：%s\n图片：%s\n户型：%s\n价格：%s'%(re.sub(r'\s+','',v[1]),v[0],re.sub(' ','',re.sub('&nbsp;','',v[2])),v[3]))
            print("\n")
    except Exception as err:
        print("err...")

    print("该页共有 " + str(len(dlist)) + " 条数据...")
    print("\n")


if __name__ == '__main__':
    while True:
        page = input("请输入租房信息页码(或输入q退出)：")
        if page == "q":
            break
        get_list(page)